package com.mxpio.erp.hr.controller;

import cn.hutool.core.date.DateUtil;
import com.mxpio.erp.common.hr.entity.AttendanceRecorder;
import com.mxpio.erp.common.hr.service.AttendanceRecorderService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "AttendanceRecorderController", description = "员工考勤每日打卡记录接口")
@RestController
@RequestMapping("/erp/hr/checkindata/")
public class AttendanceRecorderController {
	
	@Autowired
	private AttendanceRecorderService service;

	@GetMapping("list")
	@Operation(summary = "员工考勤每日打卡记录列表", description = "获取员工考勤每日打卡记录列表", method = "GET")
	public Result<List<AttendanceRecorder>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<AttendanceRecorder> tos = service.list(AttendanceRecorder.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "员工考勤每日打卡记录列表", description = "获取员工考勤每日打卡记录列表", method = "GET")
	public Result<Page<AttendanceRecorder>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<AttendanceRecorder> page = service.listPage(AttendanceRecorder.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	
	@PostMapping("add")
	@Operation(summary = "新增员工考勤每日打卡记录", description = "新增员工考勤每日打卡记录", method = "POST")
	public Result<AttendanceRecorder> add(@RequestBody AttendanceRecorder recorder) {
		service.save(recorder);
		return Result.OK(recorder);
	}
	
	@PutMapping("edit")
	@Operation(summary = "编辑员工考勤每日打卡记录", description = "编辑员工考勤每日打卡记录（全量）", method = "PUT")
	public Result<AttendanceRecorder> edit(@RequestBody AttendanceRecorder recorder) {
		service.update(recorder);
		return Result.OK(recorder);
	}

	@DeleteMapping("remove/{ids}")
	@Operation(summary = "删除", description = "删除", method = "DELETE")
	public Result<?> remove(@PathVariable(name = "ids", required = true) String ids) {
		String idps[] = ids.split(",");
		for(String key : idps){
			service.delete(AttendanceRecorder.class, key);
		}
		return Result.OK();
	}

	@GetMapping("synccheckindata/{yearMonth}")
	@Operation(summary = "员工考勤每日打卡记录从企业微信接口同步", description = "员工考勤每日打卡记录接口同步", method = "GET")
	public Result<?> syncCheckinDataFromCp(@Parameter(description = "yearMonth，格式2025-01",  required = true) @PathVariable(name = "yearMonth", required = true)  String yearMonth) {
		try {
			DateUtil.parse(yearMonth, "yyyy-MM");
		} catch (Exception e) {
			return Result.error("输入的日期格式不正确(yyyy-MM，例：2025-01)-》"+yearMonth);
		}
		String[] split = yearMonth.split("-");
		return service.syncCheckinDataFromCp(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
	}

	@GetMapping("calculate/{yearMonth}")
	@Operation(summary = "按月测算人员加班工时", description = "按月测算人员加班工时", method = "GET")
	public Result<?> calculateWorkingHours(@Parameter(description = "yearMonth，格式2025-01",  required = true) @PathVariable(name = "yearMonth", required = true)  String yearMonth) {
		try {
			DateUtil.parse(yearMonth, "yyyy-MM");
		} catch (Exception e) {
			return Result.error("输入的日期格式不正确(yyyy-MM，例：2025-01)-》"+yearMonth);
		}
		String[] split = yearMonth.split("-");
		return service.calculateWorkingHours(Integer.parseInt(split[0]),Integer.parseInt(split[1]));
	}

}
